#include "addexec.h"
#include "ui_addexec.h"

AddExec::AddExec(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::AddExec)
{
    m_ui->setupUi(this);
    QFileDialog *fileDialog=new QFileDialog(this);
    connect(m_ui->toolButton, SIGNAL(clicked()), fileDialog, SLOT(exec()));
    connect(fileDialog, SIGNAL(fileSelected(QString)), m_ui->lineEdit, SLOT(setText(QString)));

    connect(m_ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(checkPathes()));
    connect(m_ui->lineEdit_2, SIGNAL(textChanged(QString)), this, SLOT(checkPathes()));

    QFileDialog *dirDialog=new QFileDialog(this);
    connect(m_ui->toolButton_2, SIGNAL(clicked()), dirDialog, SLOT(exec()));
    connect(dirDialog, SIGNAL(fileSelected(QString)), m_ui->lineEdit_2, SLOT(setText(QString)));
}

AddExec::~AddExec(){
    delete m_ui;
}

void AddExec::on_addButton_clicked(){
    bool run=m_ui->checkBox->isChecked()?true:false;
    Exec *exec=new Exec(m_ui->lineEdit->text().toStdString(), m_ui->lineEdit_2->text().toStdString(), run);
    emit execAdded(exec);
    close();
}

void AddExec::checkPathes(){
    QFile file(m_ui->lineEdit->text());
    if(file.exists()&!m_ui->lineEdit_2->text().isEmpty())
        m_ui->addButton->setEnabled(true);
    else
        m_ui->addButton->setEnabled(false);
}


void AddExec::changeEvent(QEvent *e){
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void AddExec::on_cancelButton_clicked(){
    close();
}
